package com.jingyu.sport;

import com.jingyu.sport.domian.FirstApp;
import com.qiniu.http.Response;
import com.qiniu.rtc.RtcRoomManager;
import com.qiniu.util.Auth;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class SportApplication {
	
	private final static Logger logger = LogManager.getLogger(SportApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(SportApplication.class, args);
	}

		@Autowired
	private JdbcTemplate jdbcTemplate;


	@Autowired
	private FirstAppRepository dao;

	@RequestMapping("/")
	Object home(){
		class T{
			private int a;
			private int b;
			private int c;

			public int getA() {
				return a;
			}

			public void setA(int a) {
				this.a = a;
			}

			public int getB() {
				return b;
			}

			public void setB(int b) {
				this.b = b;
			}

			public int getC() {
				return c;
			}

			public void setC(int c) {
				this.c = c;
			}

			public T(int a, int b, int c){
				this.a = a;
				this.b	 = b;
				this.c = c;
			}
		}
		logger.info("aaaaa");
		return new T(2,4,10);
	}


	@RequestMapping("/add")
	Object add(FirstApp data){

		class T{
			public int result;
			public String message;

			public T(int result, String message) {
				this.result = result;
				this.message = message;
			}
		}

		dao.save(data);
		return new T(0,"success");
	}

	@RequestMapping("/query")
	Object query(){
		int i = jdbcTemplate.queryForObject("select count(*) from first_app",Integer.class);
		System.out.println("==========="+i);
		return "success";
	}
	
	@RequestMapping("/query1")
	Object query1() {
		return "query1";
	}
	
	@RequestMapping("/query2")
	Object query2() {
		return "query2";
	}

	@RequestMapping("/roomToken")
	String test(String userName){

		String ak = "s-2eRj0LwLspctcISwo3UGxX5SxbrOV7jT1lmkQq";
		String sk = "O4s3Ck7yfsWw0OUi4jOTVYwqKxar9AzHefC7fGUB";
		String appId = "dksmehovx";

		RtcRoomManager rtcRoomManager = new RtcRoomManager(Auth.create(ak,sk));

		Response response = null;
		String roomToken = null;
		long cur = System.currentTimeMillis();


			cur += 1000 * 60 * 24;
			cur /=1000;
		try {
			roomToken = rtcRoomManager.getRoomToken(appId,"testRoom",userName,cur,"user");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return roomToken;

	}


}
